package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.audit.HPatientAudit;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.hospital.domain.HPatientPdf;
import com.ruoyi.hospital.service.IHpatientPdfService;
import com.ruoyi.hospital.service.IHpatientService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IHPatientAuditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 患者医保--审核Controller
 *
 * @author ruoyi
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/audit/audit")
public class HPatientAuditController extends BaseController
{
    @Autowired
    private IHPatientAuditService hPatientAuditService;

    @Autowired
    private IHpatientService patientService;


    @Autowired
    private SysUserServiceImpl sysUserService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private IHpatientPdfService patientPdfService;

    /**
     * 查询患者医保--审核列表
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(HPatientAudit hPatientAudit)
    {
        startPage();
        List<HPatientAudit> list = hPatientAuditService.selectHPatientAuditList(hPatientAudit);
        return getDataTable(list);
    }



    @GetMapping("/count")
    public AjaxResult count() {


//        SysUser sysUser = new SysUser();
//        sysUser.setUserId(getUserId());
//        List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
//        System.out.println("sysUser+++++++"+sysUsers.get(0));
        Integer count = 0;
        List<SysRole> roles = roleService.selectRoleAll();
        for (SysRole sysRole : SysUser.isAdmin(getUserId()) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())) {
            if (Integer.parseInt(sysRole.getDataScope()) == 1){
                count = hPatientAuditService.selectHPatientAuditCount();
                return AjaxResult.success(count);
            }
        }
        return AjaxResult.success(count);
    }


//    @GetMapping("/gain")
//    public AjaxResult gain() {
//        Integer count = patientService.selectHPatientAuditGain();
//        return AjaxResult.success(count);
//    }


    /**
     * 导出患者医保--审核列表
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:export')")
    @Log(title = "患者医保--审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HPatientAudit hPatientAudit)
    {
        List<HPatientAudit> list = hPatientAuditService.selectHPatientAuditList(hPatientAudit);
        ExcelUtil<HPatientAudit> util = new ExcelUtil<HPatientAudit>(HPatientAudit.class);
        util.exportExcel(response, list, "患者医保--审核数据");
    }

    /**
     * 根据Id获取患者详情和报销详情
     *  @GetMapping(value = "/{configId}")
     *     public AjaxResult getInfo(@PathVariable
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:detail')")
    @GetMapping(value="/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        return success(patientService.selectById(id));
    }


    /**
     * 根据pdfId获取患者pdf文件
     *  @GetMapping(value = "/{configId}")
     *     public AjaxResult getInfo(@PathVariable
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:pdf')")
    @GetMapping("/pdf/{id}")
    public AjaxResult selectPdfById(@PathVariable Integer id) {
        HPatientPdf patientPdf = patientPdfService.getById(id);
        if(patientPdf != null){
            return success(patientPdf);
        } else {
            return error("该患者无费用结算单");
        }
    }

    /**
     * 根据inpatientNo获取患者pdf文件
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:pdf')")
    @GetMapping("/sqlSeverPdf/{id}")
    public AjaxResult getPdfFromSqlSever(@PathVariable String id) {
        HPatientPdf patientPdf = patientPdfService.selectPdfByInpatientNo(id);
        if(patientPdf != null){
            return success(patientPdf);
        } else {
            return error("该患者无费用结算单");
        }
    }

    /**
     * 新增患者医保--审核
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:add')")
    @Log(title = "患者医保--审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HPatientAudit hPatientAudit)
    {
        return toAjax(hPatientAuditService.insertHPatientAudit(hPatientAudit));
    }

    /**
     * 修改患者医保--审核
     */
    @PreAuthorize("@ss.hasPermi('audit:audit:edit')")
    @Log(title = "患者医保--审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HPatientAudit hPatientAudit)
    {
        hPatientAudit.setUpdateBy(getUserId()+"");
        hPatientAudit.setUpdateByName(getUsername());
        return toAjax(hPatientAuditService.updateHPatientAudit(hPatientAudit));
    }

//    /**
//     * 删除患者医保--审核
//     */
//    @PreAuthorize("@ss.hasPermi('audit:audit:passes')")
//    @Log(title = "患者医保--审核通过", businessType = BusinessType.DELETE)
//	@GetMapping("/passes/{ids}")
//    public AjaxResult passes(@PathVariable Long[] ids)
//    {
//        return toAjax(hPatientAuditService.passesHPatientAuditByIds(ids));
//    }

    @PreAuthorize("@ss.hasPermi('audit:audit:reject')")
    @Log(title = "患者医保--审核驳回", businessType = BusinessType.DELETE)
    @GetMapping("/reject/{ids}/{remark}")
    public AjaxResult remove(@PathVariable Long[] ids,@PathVariable String remark)
    {
        return toAjax(hPatientAuditService.deleteHPatientAuditByIds(ids));
    }
}
